<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class M_Deuda extends Modelo {

    public $id = NULL;
    public $idOrigen;
    public $matricula;
    public $tipoComprobante;
    public $signo;
    public $nroComprobante;
    public $fecha;
    public $importe;

    function __construct() {
        parent::__construct();
        $this->tabla = 'deudas';
    }

    function setAttributes($datos = array(), $tipo = 'object') {
        return parent::_setAttributes(__CLASS__, $datos, $tipo);
    }

    function doArray() {
        return parent::_doArray(__CLASS__);
    }

    /**
     * Devuelve valores por defecto en un arreglo.
     */
    function valoresPorDefecto() {
        return array(
            'idOrigen' => '',
            'matricula' => '',
            'tipoComprobante' => '',
            'signo' => '',
            'nroComprobante' => '',
            'fecha' => '',
            'importe' => '',
        );
    }

    function vaciarTabla() {
        $this->db->truncate($this->tabla);
    }

    function ultimoRegistro() {
        $query = $this->db->query("
            SELECT idOrigen 
            FROM  ssap_{$this->tabla}
            ORDER BY idOrigen DESC 
            LIMIT 0 , 1
                ");

        foreach ($query->result() as $row) {
            return $row->idOrigen;
        }
    }

    public function save() {
        $ret = '';
        $this->db->db_debug = 0;
        if ($this->id == '' || $this->id == null) {
            $this->db->insert($this->tabla, $this);
            $ret = $this->db->insert_id();
            $this->id = $ret;
        } else {
            $this->db->update($this->tabla, $this, "id = $this->id");
            $ret = $this->db->affected_rows();
        }

        if ($this->db->_error_number() != 0) {
            // capturar el error de alguna forma que sea distinto de 1062 que es el de duplicado de tabla   
            //echo $this->db->_error_message();
            return 0;
        } else {
            return $ret;
        }
    }

}